﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using FXC.Web.Service;
using System.ServiceModel.DomainServices.Client;
using FXC.SL.CS.Util;

namespace FXC.SL.UserControls.Window
{
    public partial class LoginWindow : RadWindow
    {
        FXSystemContext _context = App.Current.Resources["FXSystemContext"] as FXSystemContext;

        public LoginWindow()
        {
            InitializeComponent();
        }

        private void radButtonOK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                MessageBox.Show("请输入用户名");
                return;
            }
            if (string.IsNullOrEmpty(tbPwd.Password))
            {
                MessageBox.Show("请输入用户密码");
                return;
            }

            // remove all existing users.
            _context.Users.Clear();
            _context.Load(_context.GetUserQuery(tbName.Text,tbPwd.Password), delegate(LoadOperation<Web.Entity.User> u)
            {
                if (u.Error == null)
                {
                    if (_context.Users.Count > 0)
                    {
                        App.CurrentLoginUser = _context.Users.ElementAt(0);
                        //登录成功
                        SaveLoginInfo();
                        this.DialogResult = true;
                        ClearInput();
                        this.Close();
                    }
                    else
                    {
                        //登录失败
                        MessageBox.Show("登陆错误，请输入正确的用户名密码！");
                    }
                }
                else
                {
                    //异常
                    MessageBox.Show("登陆异常，请重新尝试!详细:" + u.Error.Message);
                    return;
                }
            }, null);
        }

        private void radButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        void SaveLoginInfo()
        {
            if (cbRem.IsChecked == true)
            {
                IsolatedStorageManager.Save(Constants.IsRememberLoginInfo, true);
                IsolatedStorageManager.Save(Constants.LogedInUserName, tbName.Text);
                IsolatedStorageManager.Save(Constants.LogedInUserPWD, tbPwd.Password);
            }
            else
            {
                IsolatedStorageManager.Delete(Constants.IsRememberLoginInfo);
                IsolatedStorageManager.Delete(Constants.LogedInUserName);
                IsolatedStorageManager.Delete(Constants.LogedInUserPWD);
            }
        }

        void RestoreSavedLoginInfo()
        {
            object a = IsolatedStorageManager.GetObjectValue(Constants.IsRememberLoginInfo);
            if (a != null && Convert.ToBoolean(a) == true)
            {
                tbName.Text = IsolatedStorageManager.GetStringValue(Constants.LogedInUserName);
                tbPwd.Password = IsolatedStorageManager.GetStringValue(Constants.LogedInUserPWD);
                cbRem.IsChecked = true;
            }
        }

        void ClearInput()
        {
            tbName.Text = string.Empty;
            tbPwd.Password = string.Empty;
        }

        private void RadWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            RestoreSavedLoginInfo();
        }
    }
}
